package com.ssm.helong.shop.service.impl;

import com.ssm.helong.shop.Dao.GoodsAttributeMapper;
import com.ssm.helong.shop.common.ServiceResultEnum;
import com.ssm.helong.shop.entity.GoodsAttribute;
import com.ssm.helong.shop.service.GoodsAttributeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.text.DateFormat;
import java.util.Date;
import java.util.Objects;

@Service
public class GoodsAttributeServiceImpl implements GoodsAttributeService {
    @Autowired
    private GoodsAttributeMapper goodsAttributeMapper;


    @Override
    public String addShopsGoods(GoodsAttribute goodsAttribute) {
        Date date = new Date();
        DateFormat df = DateFormat.getDateTimeInstance();
        goodsAttribute.setCreateTime(df.format(date));
        if(goodsAttributeMapper.insertSelective(goodsAttribute)>0){
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public String updateShopsGoods(GoodsAttribute goodsAttribute) {
        GoodsAttribute temp=goodsAttributeMapper.selectByPrimaryKey(goodsAttribute.getGoodsId());
        if(Objects.isNull(temp)){
            return ServiceResultEnum.DATA_NOT_EXIST.getResult();
        }
        Date date = new Date();
        DateFormat df = DateFormat.getDateTimeInstance();
        goodsAttribute.setUpdateTime(df.format(date));
        if(goodsAttributeMapper.updateByPrimaryKeySelective(goodsAttribute)>0){
            return ServiceResultEnum.SUCCESS.getResult();
        }
        return ServiceResultEnum.DB_ERROR.getResult();
    }

    @Override
    public GoodsAttribute getShopsGoods(Integer goodsId) {
        return goodsAttributeMapper.selectByPrimaryKey(goodsId);
    }

    /***
     * 批量修改商品上架状态（0-下架 1-上架）
     * @param goodsIds
     * @param goodsSellStatus
     * @return: java.lang.Boolean
     * @Author: helong.sun
     * @Date: 2021/5/1
     */
    @Override
    public Boolean batchUpdateSellStatus(Integer[] goodsIds, Integer goodsSellStatus) {
        return goodsAttributeMapper.batchUpdateSellStatus(goodsIds,goodsSellStatus)>0;
    }
}
